prove cbrt in CORE-MATH
原文:Correctly Rounded Cubic Root Evaluation in Double Precision — Alexei Sibidanov, Paul Zimmermann(CORE-MATH 项目)。
本文为中英双语对照翻译,并收录了原文的全部插图(图 3–5 原为四幅子图,此处按原文 2×2 版式拼合为一图);原文由 OCR 得到,其中明显的讹误(包括公式编号)已在翻译时径直修正。
The cubic root
There is a closed form solution for Eq. (1) but it already requires the cubic root function so other methods have to be employed e.g. Newton iteration.
立方根
式 (1) 虽然有闭式解,但求解过程本身就要用到立方根函数,因此必须采用其他方法,例如 Newton 迭代。
Let
is the relative error of Eq. (1) with respect to
with about two times more significant figures than in
设
就是式 (1) 相对于
其有效数字大约是
The generalization of the Newton iteration method to higher orders gives the following rule:
where each additional term reduces the error of the next approximation
把 Newton 迭代法推广到更高阶,可得如下迭代规则:
其中每增加一项,下一次近似
Since
由于
For arguments in the range [1, 2] Newton iterations for the cubic root always converges with the initial approximation
在我们的测试中,对于 [1, 2] 区间内的参数,取初始近似
The error
用二阶、三阶、四阶和五阶多项式对立方根函数做 minimax 近似的误差
The final step has to be as simple as possible so it is the second order Newton iteration (Eq. (2) and (3)) where intermediate values are represented as an unevaluated sum of two binary64 numbers so the internal precision should be about 100 bits which largely exceeds the target precision of the result of 53 bits in binary64.
最后一步应当尽可能简单,因此选用二阶 Newton 迭代(式 (2) 和式 (3)),其中间值表示为两个 binary64 数的未求和(unevaluated sum),这样内部精度约为 100 位,远超 binary64 中 53 位的目标结果精度。
The precision of the result before the final step should not hit the binary64 precision limit it should be just good enough that after the refinement—which doubles the number of significant figures—an additional refinement has to be done only in very rare cases when the rounding test fails. Based on this consideration and performance tests we select the initial cubic polynomial approximation and the third order Newton iteration step, see the top-right plots in Fig. 2 and 4.
最终步骤之前结果的精度不应触及 binary64 的精度上限,它只需足够好,使得经过精化(精化会使有效数字翻倍)之后,只有在舍入测试失败的极少数情况下才需要再做一次额外的精化。基于这一考虑以及性能测试,我们选择三次多项式初始近似加三阶 Newton 迭代步骤,见图 2 和图 4 右上角的子图。
After the refinement with the compensated algorithm, the cubic root value is represented as an unevaluated sum
经过补偿算法的精化之后,立方根的值表示为两个 binary64 数的未求和
Lemma 1
Lemma 1. Whatever the rounding mode, we have
Proof. For rounding to nearest, this is a direct consequence of the Fast2Sum algorithm, since in that case we have
引理 1. 无论舍入模式如何,都有
证明。 对于就近舍入,这是 Fast2Sum 算法的直接推论:此时
According to Lemma 1, we thus get an approximation
根据引理 1,我们得到了对立方根的一个近似
If the rounding test fails we perform an additional second order Newton iteration step starting from
如果舍入测试失败,我们就以
Unfortunately even the last refinement is not enough for the worst cases to provide the correct rounded results, fortunately there are only a few such cases so we can test arguments and return already precomputed correctly rounded values.
遗憾的是,即便做最后一次精化,对于最坏情形仍不足以给出正确舍入的结果;所幸这类情形只占极少数,因此我们可以对这些参数进行测试,并直接返回预先算好的正确舍入值。
In the round-to-nearest mode the exact cases, when both
在就近舍入模式下,当
There are 104032 distinct binary64 numbers
在 [1, 2] 区间内,共有 104032 个不同的 binary64 数
Lemma 2
Lemma 2. Let
Proof. We first deal with the special cases where
where
The corresponding equation has a single real root
where
The corresponding equation has a single real root
where
The corresponding equation has a single real root
引理 2. 设
证明。 我们先处理
其中
对应方程只有一个实根
其中
对应方程只有一个实根
其中
对应方程只有一个实根
As a consequence of Lemma 2, if the distance from the approximation
作为引理 2 的推论:如果近似
To cover the exact cases we test that the last 35 bits of x are identical then to cover the directional modes we round x to the nearest value independently of the FPU status register in the general purpose registers assuming the exact case. Then we subtract from the rounded value
为覆盖精确情形,我们先检测

Figure 1. The cubic root error
图 1. 从
1. Rounding Error Analysis
Below is the C code corresponding to the algorithm proposed above, with a cubic minimax polynomial for the initial approximation, a first cubic Newton iteration in double precision, and another classical second-order Newton iteration in double-double precision. Here zz is the input reduced to the range [1, 8), and z is reduced to [1, 2). The constants c[0]=0x1.1b0babccfef9cp-1, c[1]=0x1.2c9a3e94d1da5p-1, c[2]=-0x1.4dc30b1a1ddbap-3, c[3]=0x1.7a8d3e4ec9b07p-6. The value cvt2.f is either 1 when 0x1.428a2f98d728bp+0 of 0x1.965fea53d6e3dp+0 of
- r = 1/z
- z2 = z*z
- c0 = c[0]+z*c[1]
- c2 = c[2]+z*c[3]
- y0 = c0 + z2*c2
- y2a = y0*y0下面是对应于上述算法的 C 代码:用一个三次 minimax 多项式做初始近似,第一次立方 Newton 迭代采用双精度,另一次经典的二阶 Newton 迭代采用双倍双精度。这里 zz 是归约到区间 [1, 8) 的输入,z 归约到 [1, 2)。常数 c[0]=0x1.1b0babccfef9cp-1、c[1]=0x1.2c9a3e94d1da5p-1、c[2]=-0x1.4dc30b1a1ddbap-3、c[3]=0x1.7a8d3e4ec9b07p-6。cvt2.f 的取值是:当 0x1.428a2f98d728bp+0;当 0x1.965fea53d6e3dp+0。所有变量都是双精度,为清晰起见我们对一些变量做了重命名:
- r = 1/z
- z2 = z*z
- c0 = c[0]+z*c[1]
- c2 = c[2]+z*c[3]
- y0 = c0 + z2*c2
- y2a = y0*y0
Then the second block of instructions is:
- h0 = y2a*(y0*r) - 1
- y1 = y0 - (h0*y0)*(u0 - u1*h0)
- y1 *= cvt2.f
- y2h = y1*y1
- y2l = fma(y1,y1,-y2h)
- y3 = y2h*y1
- y3l = fma(y1,y2h,-y3) + y1*y2l
- h1 = ((y3 - zz) + y3l)*rr
- dy = h1*(y1*u0)第二段指令为:
- h0 = y2a*(y0*r) - 1
- y1 = y0 - (h0*y0)*(u0 - u1*h0)
- y1 *= cvt2.f
- y2h = y1*y1
- y2l = fma(y1,y1,-y2h)
- y3 = y2h*y1
- y3l = fma(y1,y2h,-y3) + y1*y2l
- h1 = ((y3 - zz) + y3l)*rr
- dy = h1*(y1*u0)Then y1 - dy is a good approximation of
If there are no rounding errors, the algorithm corresponds to a rational approximation
于是 y1 - dy 就是
若不存在舍入误差,该算法对应于一个有理逼近

Figure 3. The error
图 3. 针对各种初始近似、在第一次二阶 Newton 迭代步骤之后的误差
To each floating-point operation which can produce a rounding error, say
Note: we take into account that the subtraction h = y2*(y*r) - 1 is exact due to Sterbenz' theorem.
对于每一个可能产生舍入误差的浮点运算,比如
注:我们利用了减法 h = y2*(y*r) - 1 由 Sterbenz 定理保证精确这一点。
The two instructions y2h = y1*y1 and y2l = fma(y1, y1, -y2h) compute a double-double approximation y2h + y2l of y1*y1. In the rounding to nearest mode, we have exactly y2h + y2l = y1*y1. For directed rounding modes, since y1*y1 can be represented exactly with 106 bits, we can write y1*y1 = h + l with h being the rounding of y1*y1 towards zero, and l representable in double precision. If y2h = h, then y1*y1 - y2h = l and can be represented exactly, thus y2h + y2l = y1*y1. Now if y2h = nextabove(h), then y1*y1 - y2h = h + l - (h + ulp(h)) = l - ulp(h), and since ulp(l) is larger or equal to ulp(h) multiplied by ulp(h) - l is exactly representable. In summary, for all rounding modes we have y1*y1 = y2h + y2l exactly. Similarly, we have y2h*y1 = y3 + y3l exactly, thus y1*y1*y1 = y3 + y3l + delta17, where delta17 is the rounding error in y1*y2l. Since y1 is less than 2, and y2l is less than ulp(y1*y1) which is y1*y2l is bounded by y1*y2l is thus
指令 y2h = y1*y1 和 y2l = fma(y1, y1, -y2h) 计算 y1*y1 的双倍双精度近似 y2h + y2l。在就近舍入模式下,精确地有 y2h + y2l = y1*y1。对于方向舍入模式,由于 y1*y1 可以用 106 位精确表示,我们可以写成 y1*y1 = h + l,其中 h 是 y1*y1 向零舍入的结果,l 可以用双精度表示。若 y2h = h,则 y1*y1 - y2h = l 且能精确表示,因此 y2h + y2l = y1*y1。若 y2h = nextabove(h),则 y1*y1 - y2h = h + l - (h + ulp(h)) = l - ulp(h),又由于 ulp(l) 大于等于 ulp(h) 乘以 ulp(h) - l 可以精确表示。总之,对所有舍入模式,都精确地有 y1*y1 = y2h + y2l。类似地,精确地有 y2h*y1 = y3 + y3l,因此 y1*y1*y1 = y3 + y3l + delta17,其中 delta17 是 y1*y2l 的舍入误差。由于 y1 小于 2,而 y2l 小于 ulp(y1*y1)(即 y1*y2l 以 y1*y2l 上的舍入误差满足

Figure 4. The error
图 4. 针对各种初始近似、在第一次三阶 Newton 迭代步骤之后的误差
When one adds all rounding error bounds from Table 1, one gets a maximum error (due to roundings) of
把表 1 中所有的舍入误差界相加,得到最大误差(由舍入引起)为

Figure 5. The error
图 5. 针对各种初始近似、在第一次四阶 Newton 迭代步骤之后的误差
References
[1] Boldo, S., Graillat, S., and Muller, J. On the robustness of the 2Sum and Fast2Sum algorithms. ACM Trans. Math. Softw. 44, 1 (2017), 4:1–4:14.
[1] Boldo, S., Graillat, S., and Muller, J. On the robustness of the 2Sum and Fast2Sum algorithms. ACM Trans. Math. Softw. 44, 1 (2017), 4:1–4:14。(关于 2Sum 与 Fast2Sum 算法的稳健性。)
Figures 6–10 / 图 6–10

Figure 6. The error of the cubic root evaluation after the refinement step where the root
图 6. 精化步骤之后立方根求值的误差,其中根

Figure 7. The error of the cubic root evaluation for the worst case when the rounding test fails and the additional Newton iteration step is taken. FPU is operating in the round-to-nearest mode.
图 7. 在最坏情形(舍入测试失败、执行额外 Newton 迭代步骤)下立方根求值的误差。FPU 工作在就近舍入模式。

Figure 8. The error of the cubic root evaluation for the worst case when the rounding test fails and the additional Newton iteration step is taken. FPU is operating in the downward mode.
图 8. 在最坏情形(舍入测试失败、执行额外 Newton 迭代步骤)下立方根求值的误差。FPU 工作在向下舍入模式。

Figure 9. The error of the cubic root evaluation for the worst case when the rounding test fails and the additional Newton iteration step is taken. FPU is operating in the upward mode.
图 9. 在最坏情形(舍入测试失败、执行额外 Newton 迭代步骤)下立方根求值的误差。FPU 工作在向上舍入模式。

Figure 10. The error of the cubic root evaluation for the worst case when the rounding test fails and the additional Newton iteration step is taken. FPU is operating in the toward-zero mode.
图 10. 在最坏情形(舍入测试失败、执行额外 Newton 迭代步骤)下立方根求值的误差。FPU 工作在向零舍入模式。
Table 1
Table 1. The sensitivities
表 1. 算法中可能出现的所有舍入误差所对应的灵敏度
| instruction | sensitivity | |||
|---|---|---|---|---|
r=1/z | ||||
z2 = z*z | ||||
z*c[1] | ||||
c[0]+z*c[1] | ||||
z*c[3] | ||||
c[2]+z*c[3] | ||||
z2*c2 | ||||
y0=c0+z2*c2 | ||||
y2a=y0*y0 | ||||
y0*r | ||||
y2a*(y0*r) | ||||
h0*y0 | ||||
u1*h0 | ||||
u0-u1*h0 | ||||
(h0*y0)*(u0-u1*h0) | ||||
y1=y0-... | ||||
y1 *= cvt2.f | ||||
error on y1*y1*y1 | ||||
h1 = ((y3 - zz) + y3l)*rr | ||||
y1*u0 | ||||
h1*(y1*u0) | 1 |